package com.sanuo.pico.act.work;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActVrefBinding;
import com.sanuo.pico.event.ConditionState;
import com.sanuo.pico.event.Energy;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.event.P2Event;
import com.sanuo.pico.event.ReadyState;
import com.sanuo.pico.event.VV1;
import com.sanuo.pico.event.VV2;
import com.sanuo.pico.event.VbEvent;
import com.sanuo.pico.event.Vmax1;
import com.sanuo.pico.event.Vmax2;
import com.sanuo.pico.event.Vmax3;
import com.sanuo.pico.event.Vmax4;
import com.sanuo.pico.event.Vmax5;
import com.sanuo.pico.event.Vmax6;
import com.sanuo.pico.event.Vmax7;
import com.sanuo.pico.event.Vmax8;
import com.sanuo.pico.event.Vmax9;
import com.sanuo.pico.event.Vmin1;
import com.sanuo.pico.event.Vmin2;
import com.sanuo.pico.event.Vmin3;
import com.sanuo.pico.event.Vmin4;
import com.sanuo.pico.event.Vmin5;
import com.sanuo.pico.event.Vmin6;
import com.sanuo.pico.event.Vmin7;
import com.sanuo.pico.event.Vmin8;
import com.sanuo.pico.event.Vmin9;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class VrefAct extends BaseWorkActivity implements SettingPage {

    ActVrefBinding binding;
    int[] num = {40, 70, 100, 130, 160, 190, 220, 250, 280};
    List<TextView> tv = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActVrefBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        tv.add(binding.n1);
        tv.add(binding.n2);
        tv.add(binding.n3);
        tv.add(binding.n4);
        tv.add(binding.n5);
        tv.add(binding.n6);
        tv.add(binding.n7);
        tv.add(binding.n8);
        tv.add(binding.n9);

        binding.title.setText(getResources().getStringArray(R.array.settings)[3]);

        binding.back.setOnClickListener(v -> Command.sendTouchEvent(0, 0x20));
        binding.b1.setOnClickListener(v -> Command.sendTouchEvent(0, 0x39));
        binding.b2.setOnClickListener(v -> Command.sendTouchEvent(0, 0x3a));

        setTouch(binding.v1.down, () -> Command.sendTouchEvent(298));
        setTouch(binding.v1.up, () -> Command.sendTouchEvent(299));

        setTouch(binding.min1.down, () -> Command.sendTouchEvent(256));
        setTouch(binding.min1.up, () -> Command.sendTouchEvent(257));

        setTouch(binding.min2.down, () -> Command.sendTouchEvent(258));
        setTouch(binding.min2.up, () -> Command.sendTouchEvent(259));

        setTouch(binding.min3.down, () -> Command.sendTouchEvent(260));
        setTouch(binding.min3.up, () -> Command.sendTouchEvent(261));

        setTouch(binding.min4.down, () -> Command.sendTouchEvent(262));
        setTouch(binding.min4.up, () -> Command.sendTouchEvent(263));

        setTouch(binding.min5.down, () -> Command.sendTouchEvent(264));
        setTouch(binding.min5.up, () -> Command.sendTouchEvent(265));

        setTouch(binding.min6.down, () -> Command.sendTouchEvent(266));
        setTouch(binding.min6.up, () -> Command.sendTouchEvent(267));

        setTouch(binding.min7.down, () -> Command.sendTouchEvent(268));
        setTouch(binding.min7.up, () -> Command.sendTouchEvent(269));

        setTouch(binding.min8.down, () -> Command.sendTouchEvent(270));
        setTouch(binding.min8.up, () -> Command.sendTouchEvent(271));

        setTouch(binding.min9.down, () -> Command.sendTouchEvent(272));
        setTouch(binding.min9.up, () -> Command.sendTouchEvent(273));

        setTouch(binding.min10.down, () -> Command.sendTouchEvent(54));
        setTouch(binding.min10.up, () -> Command.sendTouchEvent(53));


        setTouch(binding.max1.down, () -> Command.sendTouchEvent(274));
        setTouch(binding.max1.up, () -> Command.sendTouchEvent(275));

        setTouch(binding.max2.down, () -> Command.sendTouchEvent(276));
        setTouch(binding.max2.up, () -> Command.sendTouchEvent(277));

        setTouch(binding.max3.down, () -> Command.sendTouchEvent(278));
        setTouch(binding.max3.up, () -> Command.sendTouchEvent(279));

        setTouch(binding.max4.down, () -> Command.sendTouchEvent(280));
        setTouch(binding.max4.up, () -> Command.sendTouchEvent(281));

        setTouch(binding.max5.down, () -> Command.sendTouchEvent(282));
        setTouch(binding.max5.up, () -> Command.sendTouchEvent(283));

        setTouch(binding.max6.down, () -> Command.sendTouchEvent(284));
        setTouch(binding.max6.up, () -> Command.sendTouchEvent(285));

        setTouch(binding.max7.down, () -> Command.sendTouchEvent(286));
        setTouch(binding.max7.up, () -> Command.sendTouchEvent(287));

        setTouch(binding.max8.down, () -> Command.sendTouchEvent(288));
        setTouch(binding.max8.up, () -> Command.sendTouchEvent(289));

        setTouch(binding.max9.down, () -> Command.sendTouchEvent(290));
        setTouch(binding.max9.up, () -> Command.sendTouchEvent(291));

        setTouch(binding.max10.down, () -> Command.sendTouchEvent(56));
        setTouch(binding.max10.up, () -> Command.sendTouchEvent(55));

        binding.n11.value.setVisibility(View.INVISIBLE);
        setTouch(binding.n11.down, () -> Command.sendTouchEvent(300));
        setTouch(binding.n11.up, () -> Command.sendTouchEvent(301));

        binding.save.setOnClickListener(v -> Command.sendTouchEvent(0, 0x43));
        binding.condition.setOnClickListener(v -> Command.sendTouchEvent(0, 0x3b));
        binding.ready.setOnClickListener(v -> Command.sendTouchEvent(0, 0x3c));

        onMessageEvent(Event.condition);
        onMessageEvent(Event.ready);
        onMessageEvent(Event.vb);
        onMessageEvent(Event.vv1);

        onMessageEvent(Event.vMin1);
        onMessageEvent(Event.vMin2);
        onMessageEvent(Event.vMin3);
        onMessageEvent(Event.vMin4);
        onMessageEvent(Event.vMin5);
        onMessageEvent(Event.vMin6);
        onMessageEvent(Event.vMin7);
        onMessageEvent(Event.vMin8);
        onMessageEvent(Event.vMin9);
        onMessageEvent(Event.energy);

        onMessageEvent(Event.vMax1);
        onMessageEvent(Event.vMax2);
        onMessageEvent(Event.vMax3);
        onMessageEvent(Event.vMax4);
        onMessageEvent(Event.vMax5);
        onMessageEvent(Event.vMax6);
        onMessageEvent(Event.vMax7);
        onMessageEvent(Event.vMax8);
        onMessageEvent(Event.vMax9);
        onMessageEvent(Event.p2);

        onMessageEvent(Event.vv2);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Energy event) {
        binding.min10.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ConditionState event) {
        binding.condition.setSelected(event.getValue() == 1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ReadyState event) {
        binding.ready.setSelected(event.getValue() == 1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(VV2 event) {
        binding.n10.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(VV1 event) {
        binding.v1.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(P2Event event) {
        binding.max10.value.setText(event.getValue() + "");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin1 event) {
        binding.min1.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin2 event) {
        binding.min2.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin3 event) {
        binding.min3.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin4 event) {
        binding.min4.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin5 event) {
        binding.min5.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin6 event) {
        binding.min6.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin7 event) {
        binding.min7.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin8 event) {
        binding.min8.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmin9 event) {
        binding.min9.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(VbEvent event) {
        binding.b1.setSelected(false);
        binding.b2.setSelected(false);
        for (TextView v : tv) {
            v.setText("");
        }
        if (event.getValue() == 1) {
            binding.b1.setSelected(true);
            for (int i = 0; i < tv.size(); i++) {
                tv.get(i).setText(num[i] + "");
            }
        } else if (event.getValue() == 4) {
            binding.b2.setSelected(true);
            for (int i = 0; i < tv.size(); i++) {
                tv.get(i).setText(num[i] * 2 + "");
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax1 event) {
        binding.max1.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax2 event) {
        binding.max2.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax3 event) {
        binding.max3.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax4 event) {
        binding.max4.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax5 event) {
        binding.max5.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax6 event) {
        binding.max6.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax7 event) {
        binding.max7.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax8 event) {
        binding.max8.value.setText(String.format("%.2f", event.getValue() / 100f));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Vmax9 event) {
        binding.max9.value.setText(String.format("%.2f", event.getValue() / 100f));
    }
}
